import { add, edit, detail, list, del } from "../../api/index.js";
Page({
  data: {
    block: false,
    restaurant: false,
    check: true,
    goodList: [],
    totalPrice: 0,
    address: {},
    envelopeId: null,
    envelope: {},
    envelopeNum: 0,
  },
  onLoad(options) {
    var that = this;
    wx.getStorage({
      key: "detailData",
      success: function (res) {
        console.log("detailData", res.data);
        that.setData({
          goodList: res.data,
        });
        console.log("goodList", that.data.goodList);
        that.data.goodList.forEach((item) => {
          console.log("item", item);

          totalPrice += Number(item.totalMoney);
        });
        that.setData({
          totalPrice: Number(totalPrice),
        });
      },
    });
    let totalPrice = 0;
  },
  onShow() {
    const app = getApp();
    if (app.globalData.backData) {
      console.log("收到返回数据:", app.globalData.backData);
      this.setData({
        envelopeId: app.globalData.backData.envelope,
      });
      app.globalData.backData = null;
    }
    this.getAddress();
    this.getEnvelopeList();
    if (this.data.envelopeId) {
      this.getEnvelope();
    }
  },
  async getEnvelope() {
    // cosnt id
    const res = await detail(this.data.envelopeId, "envelope");
    if (res.code === 200) {
      this.setData({
        envelope: res.data,
      });
    }
  },
  async getEnvelopeList() {
    var that = this;
    const params = {
      pageNum: 1,
      pageSize: 100,
      userId: wx.getStorageSync("userInfo").userId,
    };
    const table = "envelope";
    const res = await list(params, table);
    if (res.code == 200) {
      let num = 0;
      res.rows.forEach((item) => {
        if (item.conditionPrice <= that.data.totalPrice) {
          num++;
        }
      });
      this.setData({
        envelopeNum: num,
      });
    }
  },
  async getAddress() {
    const res = await list(
      {
        userId: wx.getStorageSync("userInfo").userId,
        standby1: 1,
      },
      "address"
    );
    if (res.code === 200) {
      this.setData({
        address: res.rows[0],
      });
    }
  },
  calling: function () {
    wx.makePhoneCall({
      phoneNumber: "1",
      success: function () {
        console.log("拨打电话成功！");
      },
      fail: function () {
        console.log("拨打电话失败！");
      },
    });
  },
  Block: function () {
    this.setData({
      block: true,
    });
  },
  takeOut: function () {
    this.setData({
      restaurant: false,
    });
  },
  Cancel: function () {
    this.setData({
      block: false,
      check: true,
      restaurant: false,
    });
  },
  Ok: function () {
    var that = this;
    this.setData({
      block: false,
    });
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res);
        that.setData({
          restaurant: true,
        });
      },
      fail: (res) => {
        that.setData({
          restaurant: false,
          check: true,
        });
      },
    });
  },
  toMyPackage: function () {
    wx.navigateTo({
      url: "../myPackage/myPackage?price=" + this.data.totalPrice,
    });
  },
  toMyAddress: function () {
    wx.navigateTo({
      url: "../address/address",
    });
  },
  async submitOrder() {
    console.log("提交订单");
    const orderNumber = Date.now();
    console.log("orderNumber", orderNumber);

    const table = "order";
    const data = {
      // goodsId: this.data.goodList[0].id,
      // goodName: this.data.goodList[0].name,
      // number: this.data.goodList[0].num,
      // goodPhoto: this.data.goodList[0].img,
      // price: this.data.totalPrice,
      userId: wx.getStorageSync("userInfo").userId,
      orderNumber: orderNumber,
      paymentAmount: this.data.totalPrice - (this.data.envelope.fullPrice || 0),
      addressId: this.data.address.id,
      envelopeId: this.data.envelopeId,
      // standby1: 0,
    };
    const res = await add(data, table);
    if (res.code === 200) {
      this.data.goodList.forEach(async (item) => {
        const orderData = {
          orderId: orderNumber,
          goodsId: item.id,
          goodName: item.name,
          standby1: item.num,
          goodPhoto: item.img,
          currentPrice: item.totalMoney,
        };
        const res1 = await add(orderData, "detail");
        if (res1.code === 200) {
          const res2 = await del(item.id, "cart");
        }
      });

      wx.showToast({
        title: "购买成功",
        icon: "success",
        duration: 2000,
      });
      wx.navigateBack({});
    }
    wx.requestPayment({
      timeStamp: "",
      nonceStr: "",
      package: "",
      signType: "MD5",
      paySign: "",
      success: function (res) {},
      fail: function (res) {},
    });
  },
});
